#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<string>
#include<vector>
#include<stack>
using namespace std;

class Solution {
public:
    string ReverseSentence(string str)
    {
        string s;
        string st2;
        if (str.size() == 0)
            return str;
        auto it = str.begin();
        while (it != str.end())
        {
            st2 += *it;

            if (*it == ' ' && it != str.end())
            {
                st2.pop_back();
                s.insert(0, ' ' + st2);
                st2.clear();
            }
            ++it;
        }
        s.insert(0, st2);
        return s;
    }
};